/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fat.euro.beans;

import com.mysql.jdbc.Constants;
import fat.euro.entities.AndroidMessage;
import fat.euro.entities.Client;
import fat.euro.jpa.ClientJpaController;
import fat.euro.utils.GoogleConstants;
import fat.euro.utils.MessageUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.Persistence;
import javax.transaction.UserTransaction;

/**
 *
 * @author VAIO
 */
@ManagedBean
@RequestScoped
public class PushMessage {
    private String message;
    private String password;
    private List<AndroidMessage> listMsg;
    private ClientJpaController jpa;
    private String key;
    private String currentKey;
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getCurrentKey() {
        return currentKey;
    }

    public void setCurrentKey(String currentKey) {
        this.currentKey = currentKey;
    }
    
    /**
     * Creates a new instance of PushMessage
     */
    public PushMessage() {
        try {
            InitialContext ctx=new InitialContext();
            UserTransaction utx = (UserTransaction) ctx.lookup("java:comp/UserTransaction");
            jpa=new ClientJpaController(utx,Persistence.createEntityManagerFactory("FatPushServerPU"));
            key=GoogleConstants.getAUTH_KEY("fptaptech");
            listMsg= getAndroidMessage();
        } catch (IOException ex) {
            Logger.getLogger(PushMessage.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(ClientManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public String push() {
        try {
            key=GoogleConstants.getAUTH_KEY(password);
        } catch (IOException ex) {
            Logger.getLogger(PushMessage.class.getName()).log(Level.SEVERE, null, ex);
        }
        listMsg= getAndroidMessage();
        for(AndroidMessage m:listMsg) {
            try {
                int code=MessageUtils.sendMessage(key,m.getRegistrationKey(), message);
                System.out.println(":"+code);
            } catch (IOException ex) {
                Logger.getLogger(PushMessage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return "";
    }
    
    public String pushClient() {
        
            try {
                AndroidMessage m=new AndroidMessage(currentKey, message);
                m.setAuthKey(key);
                int code=MessageUtils.sendMessage(key,m.getRegistrationKey(), message);
                System.out.println(":"+code);
            } catch (IOException ex) {
                Logger.getLogger(PushMessage.class.getName()).log(Level.SEVERE, null, ex);
            }
        return "";
    }
    
    public List<AndroidMessage> getAndroidMessage() {
        List<Client> list=jpa.findClientEntities();
        listMsg=new ArrayList<AndroidMessage>();
        if(list!=null) {
            for(Client c:list) {
                AndroidMessage msg=new AndroidMessage(c.getRegistrationId(), "");
                msg.setAuthKey(key);
                listMsg.add(msg);
            }
        }
        return listMsg;
    }
    
}
